package com.smarttest.portal.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smarttest.portal.mapper.CaseGroupMapper;
import com.smarttest.portal.pojo.Authgroup;
import com.smarttest.portal.pojo.AuthgroupExample;
import com.smarttest.portal.pojo.Authuser;
import com.smarttest.portal.pojo.CaseGroup;
import com.smarttest.portal.pojo.CaseGroupExample;
import com.smarttest.portal.pojo.CaseGroupExample.Criteria;
import com.smarttest.portal.pojo.CaseGroupWithBLOBs;
import com.smarttest.portal.service.RunTestService;
//老的运行测试类，不用了
@Service
public class RunTestServiceImpl implements RunTestService {
	@Autowired
	private CaseGroupMapper caseGroupMapper;
	@Value("${interface}")
	protected String TEST_TYPE_INTERFACE;	

	@Value("${protocol}")
	protected String TEST_TYPE_PROTOCOL;
	@Override
	public String getTEST_TYPE_INTERFACE() {
		return TEST_TYPE_INTERFACE;
	}
	@Override
	public String getTEST_TYPE_PROTOCOL() {
		return TEST_TYPE_PROTOCOL;
	}
	private static CloseableHttpClient httpclient;
	static {
		httpclient = HttpClients.createDefault();
	}
	@Override
	public JSONObject runTest(String excutetime, String cases, String mailers, String release, String runjob) {
		// TODO Auto-generated method stub
		String url = "http://172.22.0.69:8081/testG7S/RunTestServlet?excutetime="+excutetime+"&right="+cases+"&right2="+mailers+"&run_release="+release+"&runJob="+runjob+"#";
		CloseableHttpResponse response = null;
		HttpPost httppost = new HttpPost(url);
		JSONObject jsonobj = null;
		try {
			response = httpclient.execute(httppost);		
			HttpEntity myEntity = response.getEntity();
			String resString = EntityUtils.toString(myEntity, "utf-8");
			jsonobj = JSON.parseObject(resString);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return jsonobj;
	}
	@Override
	public List<CaseGroupWithBLOBs> getCase(String groupname) {
		String []grps = groupname.split(",");
		// TODO Auto-generated method stub
		CaseGroupExample example = new CaseGroupExample();
		Criteria critera = example.createCriteria();
		for (String s : grps) {
			critera.andGroupnameEqualTo(s);
		}
		List<CaseGroupWithBLOBs> cases = caseGroupMapper.selectByExampleWithBLOBs(example);
		return cases;
	}
}
